package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.InvokeUtil;
import cn.e4j.modular.crm.entity.GwcuT;
import cn.e4j.modular.crm.entity.MmraT;
import cn.e4j.modular.crm.mapper.MmraTMapper;
import cn.e4j.modular.crm.service.MmraTService;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  客户设备档案信息维护 服务实现类
 * </p>
 *
 * @author miaoyl
 * @since 2019-01-16
 */
@Service
public class MmraTServiceImpl extends ServiceImpl<MmraTMapper, MmraT> implements MmraTService {

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getMmralist(Page<Object> page, Dto dto) {
//        List<Map<String, Object>> mmraDtos = this.baseMapper.listMmraPage(page, dto);
        List<Map<String, Object>> mmraDtos = InvokeUtil.invokeBeanMethod("mmraTMapper,listMmraPage",page,dto);
        return mmraDtos;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getGwamList(Page<Object> page, Dto dto) {
        List<Map<String, Object>> gwamDtos = this.baseMapper.listGwamPage(page, dto);
        return gwamDtos;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getGwamAllList(Page<Object> page,Dto dto) {
        List<Map<String, Object>> gwamDtos = this.baseMapper.listGwam(page,dto);
        return gwamDtos;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getSysOrgList(Page<Object> page, Dto dto) {
        List<Map<String, Object>> orgDtos = this.baseMapper.listSysOrgPage(page, dto);
        return orgDtos;
    }

    @Transactional(readOnly = true)
    public List<MmraT> getByKey(MmraT mmraT) {
        ShiroUser shiroUser = ShiroKit.getUser();
        UpdateWrapper<MmraT> mmraTUpdateWrapper = new UpdateWrapper<>();
        mmraTUpdateWrapper.eq("MMRAENT",shiroUser.getDBC());
        //mmraTUpdateWrapper.eq("MMRA001",mmraT.getMMRA001());
        mmraTUpdateWrapper.eq("MMRA002",mmraT.getMMRA002());
        mmraTUpdateWrapper.eq("MMRA003",mmraT.getMMRA003());
        List<MmraT> mmra = this.baseMapper.selectList(mmraTUpdateWrapper);
        return mmra;
    }

    @Transactional
    @Override
    public void add(MmraT mmraT) {
        ShiroUser shiroUser = ShiroKit.getUser();
        mmraT.setMMRA007(" ");
        mmraT.setMMRA014("1");
        mmraT.setMMRAENT(shiroUser.getDBC());
        mmraT.setMMRAUSER(shiroUser.getACCOUNT());
        mmraT.setMMRAGRUP(shiroUser.getORG_ID());
        mmraT.setMMRADATE(LocalDate.now());
        mmraT.setMMRATIME(DateUtils.formatDate(new Date(), "HH:mm:ss"));
        mmraT.setMMRAORIG(shiroUser.getORG_ID());
        mmraT.setMMRAORIU(shiroUser.getACCOUNT());

        mmraT.insert();
    }

    @Transactional
    @Override
    public void edit(MmraT mmraT,MmraT oldMmraT) {
        ShiroUser shiroUser = ShiroKit.getUser();
        mmraT.setMMRAMODU(shiroUser.getACCOUNT());
        UpdateWrapper<MmraT> mmraTUpdateWrapper = new UpdateWrapper<>();
        mmraTUpdateWrapper.eq("MMRAENT",mmraT.getMMRAENT());
        mmraTUpdateWrapper.eq("MMRA001",oldMmraT.getMMRA001());
        mmraTUpdateWrapper.eq("MMRA002",oldMmraT.getMMRA002());
        mmraTUpdateWrapper.eq("MMRA003",oldMmraT.getMMRA003());

        mmraT.update(mmraTUpdateWrapper);
    }


    @Transactional
    @Override
    public void delete(MmraT mmraT) {
        UpdateWrapper<MmraT> mmraTUpdateWrapper = new UpdateWrapper<>();
        mmraTUpdateWrapper.eq("MMRAENT",mmraT.getMMRAENT());
        mmraTUpdateWrapper.eq("MMRA001",mmraT.getMMRA001());
        mmraTUpdateWrapper.eq("MMRA002",mmraT.getMMRA002());
        mmraTUpdateWrapper.eq("MMRA003",mmraT.getMMRA003());

        GwcuT gwcuT = new GwcuT();
        UpdateWrapper<GwcuT> gwcuTUpdateWrapper = new UpdateWrapper<>();
        gwcuTUpdateWrapper.eq("GWCUENT",mmraT.getMMRAENT());
        gwcuTUpdateWrapper.eq("GWCU001",mmraT.getMMRA002());
        gwcuTUpdateWrapper.eq("GWCU002",mmraT.getMMRA003());
        gwcuTUpdateWrapper.eq("GWCU012",mmraT.getMMRA001());
        gwcuTUpdateWrapper.eq("GWCU030",mmraT.getMMRA019());
        gwcuT.delete(gwcuTUpdateWrapper);

        mmraT.delete(mmraTUpdateWrapper);
    }

    @Transactional
    @Override
    public void delDataBatch(List<MmraT> mmraTList) {
        for(MmraT mmraT : mmraTList){
            UpdateWrapper<MmraT> mmraTUpdateWrapper = new UpdateWrapper<>();
            mmraTUpdateWrapper.eq("MMRAENT",mmraT.getMMRAENT());
            mmraTUpdateWrapper.eq("MMRA001",mmraT.getMMRA001());
            mmraTUpdateWrapper.eq("MMRA002",mmraT.getMMRA002());
            mmraTUpdateWrapper.eq("MMRA003",mmraT.getMMRA003());

            GwcuT gwcuT = new GwcuT();
            UpdateWrapper<GwcuT> gwcuTUpdateWrapper = new UpdateWrapper<>();
            gwcuTUpdateWrapper.eq("GWCUENT",mmraT.getMMRAENT());
            gwcuTUpdateWrapper.eq("GWCU001",mmraT.getMMRA002());
            gwcuTUpdateWrapper.eq("GWCU002",mmraT.getMMRA003());
            gwcuTUpdateWrapper.eq("GWCU012",mmraT.getMMRA001());
            gwcuTUpdateWrapper.eq("GWCU030",mmraT.getMMRA019());
            gwcuT.delete(gwcuTUpdateWrapper);

            mmraT.delete(mmraTUpdateWrapper);
        }
    }

    @Override
    public List<Map<String, Object>> listGwemPage(Page<Object> page, Dto dto) {
        return this.baseMapper.listGwemPage(page,dto);
    }
    @Override
    public List<Dto> getGwemData(Dto dto){
        return this.baseMapper.getGwemData(dto);
    }
}
